package useless.wecker;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Schalter {


    //public static String LICHT_1 = "1";
    private static String ON = "1";
    private static String OFF = "0";
    private static int sendCounter = 0;

    public Schalter() throws Exception{
        throw new Exception("Dont you instantiate this class!");
    }
    
    public static void allesAn(){
        an(Dose.KAFFEE);
        an(Dose.LICHT1);
    }
    public static void kaffeeAn(){
        an(Dose.KAFFEE);
    }
    public static void kaffeeAus(){
        aus(Dose.KAFFEE);
    }
    
    public static enum Dose {

        LICHT1(3),
        KAFFEE(2);
        private int value;

        private Dose(int value) {
            this.value = value;
        }

        public String getValue() {
            int x = 4;
            return String.valueOf(this.value);
        }
    };

    
    public static void an(Dose dose) {
        try{
        sendCounter += 1;
        send(dose, ON);
        }catch(IOException ex){
            System.out.println("IOException occured while switching Dose " + dose.toString() + " on. Try " + sendCounter);
            if (sendCounter <= UselessWecker.MAX_RESEND){
                System.out.println("retrying ...");
                an(dose);
            }
            else{
                System.out.println("aborting ...");
            }
        }
        catch(InterruptedException ex){
            System.out.println("IOException occured while switching Dose " + dose.toString() + " on. Try " + sendCounter);
            if (sendCounter <= UselessWecker.MAX_RESEND){
                System.out.println("retrying ...");
                an(dose);
            }
            else{
                System.out.println("aborting ...");
            }            
        }
    }

    public static void aus(Dose dose) {
        try{
        sendCounter += 1;
        send(dose, OFF);
        }catch(IOException ex){
            System.out.println("IOException occured while switching Dose " + dose.toString() + " off. Try " + sendCounter);
            if (sendCounter <= UselessWecker.MAX_RESEND){
                System.out.println("retrying ...");
                an(dose);
            }
            else{
                System.out.println("aborting ...");
            }
        }catch(InterruptedException ex){
            System.out.println("IOException occured while switching Dose " + dose.toString() + " off. Try " + sendCounter);
            if (sendCounter <= UselessWecker.MAX_RESEND){
                System.out.println("retrying ...");
                an(dose);
            }
            else{
                System.out.println("aborting ...");
            }
        }
    }

    private static void send(Dose dose, String onoff) throws IOException, InterruptedException {
        String cmd = "send " + UselessWecker.BASE_CODE + " " + dose.getValue() + " " + onoff;
        if (UselessWecker.DEBUG_MODE == 0) {

            Runtime run = Runtime.getRuntime();
            Process pr = run.exec(cmd);
            pr.waitFor();
        }
        Terminal.SET_LAST_COMMAND(cmd);
    }

}
